<?php
namespace App\Command;

use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Game\Generator\ItemGenerator;
use App\Globals;

class GenerateItem extends Command
{
    protected function configure()
    {
        $this
            ->setName('generate:item')
            ->setDescription('Generate item')
            ->addArgument('type', InputArgument::REQUIRED, 'Item type (weapon)')
            ->addArgument('title', InputArgument::REQUIRED, 'Item title')
            ->addArgument('owner_id', InputArgument::OPTIONAL, 'Id of item owner')
        ;
    }

    protected function execute(InputInterface $input, OutputInterface $output)
    {
        $params = array();
        $params['title'] = $input->getArgument('title');
        $type = $input->getArgument('type');

        $gen = new ItemGenerator();
        $item = $gen->generate($type, $params);

        $em = Globals::init()->getEntityManager();

        $owner_id = $input->getArgument('owner_id');
        if( $owner_id )
        {
            $char = $em->find('Game\Entity\Character', $owner_id);
            if( !$char ) throw new \Exception(sprintf('Character with id=%s not found', $owner_id));
            $item->setCharacter($char);
        }

        $em->persist($item);
        $em->flush();

        $text = sprintf('<info>Item "%s" with id=%s created</info>', $item->getTitle(), $item->getId());

        $output->writeln($text);
    }
}